package com.company._2Field;

import com.company._class01.Person;

import java.lang.reflect.Field;

public class TestFiledDemo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Class c= Person.class;
//得到公开字段
//        Field[] fs1=c.getFields();
//        for(Field f:fs1){
//            System.out.println(f);
//        }
        //得到所有本类字段
//        Field[] fs2=c.getDeclaredFields();
//        for(Field f:fs2){
//            System.out.println(f);
//        }
        Field f1=c.getField("name1");
        Field f2=c.getDeclaredField("age");
        f2.setAccessible(true);
        Person p=new Person();
        f1.set(p,"老王");
        f2.set(p,40);

    }
}
